package org.jeecg.modules.consumptionRecycledwater.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.chargeWater.service.IChargeWaterService;
import org.jeecg.modules.consumptionRecycledwater.entity.ConsumptionRecycledwater;
import org.jeecg.modules.consumptionRecycledwater.entity.RecycledSelectDTO;
import org.jeecg.modules.consumptionRecycledwater.service.IConsumptionRecycledwaterService;
import org.jeecg.modules.feeRecycled.entity.FeeRecycled;
import org.jeecg.modules.feeRecycled.service.IFeeRecycledService;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.math.MathContext;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @Description: 中水能耗
 * @Author: jeecg-boot
 * @Date:   2023-06-20
 * @Version: V1.0
 */
@Api(tags="中水能耗")
@RestController
@RequestMapping("/consumptionRecycledwater/consumptionRecycledwater")
@Slf4j
public class ConsumptionRecycledwaterController extends JeecgController<ConsumptionRecycledwater, IConsumptionRecycledwaterService> {
	@Autowired
	private IConsumptionRecycledwaterService consumptionRecycledwaterService;
	@Autowired
	private IChargeWaterService chargeWaterService;
	@Autowired
	private IFeeRecycledService feeRecycledService;
	
	/**
	 * 分页列表查询
	 *
	 * @param consumptionRecycledwater
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "中水能耗-分页列表查询")
	@ApiOperation(value="中水能耗-分页列表查询", notes="中水能耗-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(ConsumptionRecycledwater consumptionRecycledwater,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<ConsumptionRecycledwater> queryWrapper = QueryGenerator.initQueryWrapper(consumptionRecycledwater, req.getParameterMap());
		Page<ConsumptionRecycledwater> page = new Page<ConsumptionRecycledwater>(pageNo, pageSize);
		IPage<ConsumptionRecycledwater> pageList = consumptionRecycledwaterService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	 @PostMapping("/select")
	 public Result<?> select(@RequestBody RecycledSelectDTO selectDTO){
		 List<ConsumptionRecycledwater> list = consumptionRecycledwaterService.selectByTimeAndDept(selectDTO);
		 return Result.OK(list);
	 }

	 @GetMapping("/getAll")
	 public Result<?> getAll(){
		 Double all = consumptionRecycledwaterService.getAll();
		 return Result.OK(all);
	 }

	@GetMapping("/getAllGroupByDept")
	public Result<?> getAllGroupByDept(){
		List<Map<String, Object>> list = consumptionRecycledwaterService.getAllGroupByDept();
		return Result.OK(list);
	}
	
	/**
	 *   添加
	 *
	 * @param consumptionRecycledwater
	 * @return
	 */
	@AutoLog(value = "中水能耗-添加")
	@ApiOperation(value="中水能耗-添加", notes="中水能耗-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody ConsumptionRecycledwater consumptionRecycledwater) {
		setDate(consumptionRecycledwater);
		FeeRecycled feeRecycled = getFeeRecycled(consumptionRecycledwater);
		feeRecycledService.save(feeRecycled);
		consumptionRecycledwaterService.save(consumptionRecycledwater);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param consumptionRecycledwater
	 * @return
	 */
	@AutoLog(value = "中水能耗-编辑")
	@ApiOperation(value="中水能耗-编辑", notes="中水能耗-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody ConsumptionRecycledwater consumptionRecycledwater) {
		setDate(consumptionRecycledwater);
		FeeRecycled feeRecycled = getFeeRecycled(consumptionRecycledwater);
		feeRecycledService.updateById(feeRecycled);
		consumptionRecycledwaterService.updateById(consumptionRecycledwater);
		return Result.OK("编辑成功!");
	}

	@NotNull
	private FeeRecycled getFeeRecycled(ConsumptionRecycledwater consumptionRecycledwater) {
		FeeRecycled feeRecycled = new FeeRecycled();
		BigDecimal consumption = new BigDecimal(consumptionRecycledwater.getConsumption(), new MathContext(2));
		Map<String, Object> map = chargeWaterService.getRecycledProgramme();
		BigDecimal price = (BigDecimal) map.get("price");
		BigDecimal multiply = consumption.multiply(price, new MathContext(2));
		feeRecycled.setId(consumptionRecycledwater.getId()).setFee(multiply).setDept(consumptionRecycledwater.getDept()).setYear(consumptionRecycledwater.getYear())
				.setMonth(consumptionRecycledwater.getMonth()).setDate(consumptionRecycledwater.getDate());
		return feeRecycled;
	}

	private static void setDate(ConsumptionRecycledwater consumptionRecycledwater) {
		if(StringUtils.isNotEmpty(consumptionRecycledwater.getMonth())&&StringUtils.isNotEmpty(consumptionRecycledwater.getYear())){
			String month=String.format("%2s",consumptionRecycledwater.getMonth()).replace(" ", "0");
			String date= consumptionRecycledwater.getYear()+month;
			consumptionRecycledwater.setDate(Integer.parseInt(date));
		}
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "中水能耗-通过id删除")
	@ApiOperation(value="中水能耗-通过id删除", notes="中水能耗-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		consumptionRecycledwaterService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "中水能耗-批量删除")
	@ApiOperation(value="中水能耗-批量删除", notes="中水能耗-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.consumptionRecycledwaterService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "中水能耗-通过id查询")
	@ApiOperation(value="中水能耗-通过id查询", notes="中水能耗-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		ConsumptionRecycledwater consumptionRecycledwater = consumptionRecycledwaterService.getById(id);
		if(consumptionRecycledwater==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(consumptionRecycledwater);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param consumptionRecycledwater
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, ConsumptionRecycledwater consumptionRecycledwater) {
        return super.exportXls(request, consumptionRecycledwater, ConsumptionRecycledwater.class, "中水能耗");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, ConsumptionRecycledwater.class);
    }

}
